package com.jms.service.cms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jms.dao.BaseDaoI;
import com.jms.model.cms.Tcate;
import com.jms.model.sys.Tresource;
import com.jms.pageModel.base.PageFilter;
import com.jms.pageModel.base.Tree;
import com.jms.pageModel.cms.Cate;
import com.jms.service.cms.CateServiceI;
import com.jms.utils.StringUtil;

@Service
public class CateServiceImpl implements CateServiceI {

	@Autowired
	private BaseDaoI<Tcate> cateDao;
	
	@Override
	public List<Tree> tree() {
		List<Tcate> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = cateDao.find("from Tcate t order by t.seq");

		if ((l != null) && (l.size() > 0)) {
			for (Tcate t : l) {
				Tree tree = new Tree();
				tree.setId(t.getId().toString());
				if (t.getCate() != null) {
					tree.setPid(t.getCate().getId().toString());
				}
				tree.setText(t.getName());
				tree.setIconCls(t.getIcon());
				lt.add(tree);
			}
		}
		return lt;
	}
	
	@Override
	public List<Cate> treeGrid() {
		List<Cate> lr = new ArrayList<Cate>();
		List<Tcate> l = cateDao
				.find("from Tcate t left join fetch t.cate  order by t.seq");
		if ((l != null) && (l.size() > 0)) {
			for (Tcate t : l) {
				Cate c = new Cate();
				BeanUtils.copyProperties(t, c);
				if (t.getCate() != null) {
					c.setPid(t.getCate().getId());
					c.setPname(t.getCate().getName());
				}
				c.setIconCls(t.getIcon());
				lr.add(c);
			}
		}
		return lr;
	}
	
	@Override
	public List<Cate> dataGrid(Cate cate, PageFilter ph) {
		List<Cate> catelist = new ArrayList<Cate>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "from Tcate t ";
		List<Tcate> l = cateDao.find(hql+whereHql(cate, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tcate t : l) {
			Cate c = new Cate();
			StringUtil.beanCopy(t, c, true);
			catelist.add(c);
		}
		
		return catelist;

	}
	public String whereHql(Cate cate,Map<String, Object> params){
		String hql = "";
		if (cate != null) {
			hql += " where 1=1 ";
			if (cate.getName() != null) {
				hql += " and t.title like :title";
				params.put("title", "%%" + cate.getName() + "%%");
			}
		}
		return hql;
	}
	public String orderHql(PageFilter ph){
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	@Override
	public Long count(Cate cate, PageFilter ph) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tcate t ";
		return cateDao.count("select count(*) " + hql + whereHql(cate, params), params);
		
	}

	@Override
	public void add(Cate cate) {
		Tcate t = new Tcate();
		StringUtil.beanCopy(cate, t, true);
		if(cate.getPid()!=null){
			t.setCate(cateDao.get(Tcate.class, cate.getPid()));
		}
		cateDao.saveOrUpdate(t);
		
	}

	@Override
	public void delete(Long id) {
		Tcate t = cateDao.get(Tcate.class, id);
		if(t.getCates()!=null){
			System.out.println("有子节点");
		}else{
			del(t);
		}
		
	}
	private void del(Tcate t) {
		cateDao.delete(t);
	}
	@Override
	public void edit(Cate cate) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Cate get(Long id) {
		Tcate t = cateDao.get(Tcate.class, id);
		Cate c = new Cate();
		BeanUtils.copyProperties(t, c);
		if (t.getCate() != null) {
			c.setPid(t.getCate().getId());
			c.setPname(t.getCate().getName());
		}
		if(t.getCates()!=null){
			Iterator<Tcate> it = t.getCates().iterator();
			List list = new ArrayList();
			while(it.hasNext()){
				list.add(it.next().getLevel());
			}
			c.setChilds(list);
		}
		return c;
	}

	@Override
	public List<Tree> allTree(boolean flag) {
		List<Tcate> l = null;
		List<Tree> lt = new ArrayList<Tree>();
		l = cateDao.find("select distinct t from Tcate t left join fetch t.cate  order by t.seq");
		
		if ((l != null) && (l.size() > 0)) {
			for (Tcate r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getCate() != null) {
					tree.setPid(r.getCate().getId().toString());
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				attr.put("alias", r.getAlias());
				tree.setAttributes(attr);
				lt.add(tree);
			}
		}
		return lt;
	}
	
	


	
}
